package com.attendance.controller;

import com.attendance.common.CommonResult;
import com.attendance.pojo.Class;
import com.attendance.pojo.ProjectGroup;
import com.attendance.pojo.Student;
import com.attendance.service.ProjectGroupService;
import com.attendance.vo.ProjectGroupVO;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/group")
public class ProjectGroupController {

    @Autowired
    private ProjectGroupService projectGroupService;

    /**
     * 分页处理项目组信息列表
     * @param pn
     * @return
     */
    @GetMapping
    public CommonResult selectAll(@RequestParam(value = "pn",defaultValue = "1")Integer pn){
        PageHelper.startPage(pn,5);
        List<ProjectGroupVO> list=projectGroupService.selectAll();
        if (list.size() == 0){
            return CommonResult.failed("查询失败");
        }
        PageInfo<ProjectGroupVO> page=new PageInfo<>(list);
        return CommonResult.success(page,"查询成功");
    }

    /**
     * 分页处理根据输入查询的项目组信息列表
     * @param groName
     * @param stuName
     * @param stuNum
     * @return
     */
    @GetMapping("/selectByName")
    public CommonResult selectProjectGroup(String groName,String stuName,String stuNum,
                                           @RequestParam(value = "pn",defaultValue = "1")Integer pn){
        PageHelper.startPage(pn,5);
        List<ProjectGroupVO> list=projectGroupService.selectProjectGroup(groName,stuName,stuNum);
        if (list.size() == 0){
            return CommonResult.failed("查询失败");
        }
        PageInfo<ProjectGroupVO> page = new PageInfo<>(list);
        return CommonResult.success(page,"查询成功");
    }

    /**
     * 新增分组
     * @param projectGroup
     * @return
     */
    @PostMapping("/addpg")
    public CommonResult addProjectGroup(@RequestBody ProjectGroup projectGroup){
        int flag=projectGroupService.addProjectGroup(projectGroup);
        if (flag==1){
            return CommonResult.success("新增成功");
        }else {
            return CommonResult.failed("新增失败");
        }
    }

    /**
     * 新增学生
     * @param id
     * @return
     */
//    @PostMapping("/add")
//    public CommonResult add(@RequestBody Class class1, Student student){
//        int flag1=projectGroupService.addProjectGroupcl(class1);
//        int flag2=projectGroupService.addProjectGroupstu(student);
//        if (flag1==1&&flag2==1){
//            return CommonResult.success("新增成功");
//        }else {
//            return CommonResult.failed("新增失败");
//        }
//    }
//
    /**
     * 根据id删除项目组信息
     * @param id
     * @return
     */
    @DeleteMapping("{id}")
    public CommonResult deleteProjectGroup(@PathVariable String id){
        int flag=projectGroupService.deleteProjectGroup(id);
        if (flag==1){
            return CommonResult.success("删除成功");
        }else {
            return CommonResult.failed("删除失败");
        }
    }

    /**
     * 修改项目组信息
     * @param projectGroup
     * @return
     */
    @PutMapping
    public CommonResult updateProjectGroup(@RequestBody ProjectGroup projectGroup){
        int flag=projectGroupService.updateProjectGroup(projectGroup);
        if (flag==1){
            return CommonResult.success("修改成功");
        }else {
            return CommonResult.failed("修改失败");
        }
    }

    /**
     * 修改状态(是否为班长)
     * @param pgId
     * @return
     */
    @PutMapping("/modifyStatus1/{pgId}")
    public CommonResult modifyStatus1(@PathVariable String pgId){
        int flag=projectGroupService.modifyStatue1(pgId);
        if (flag==1){
            return CommonResult.success("修改状态成功");
        }else {
            return CommonResult.failed("修改状态失败");
        }
    }

    /**
     * 修改状态(项目是否结业)
     * @param stuId
     * @return
     */
    @PutMapping("/modifyStatus2/{stuId}")
    public CommonResult modifyStatus2(@PathVariable String stuId){
        int flag=projectGroupService.modifyStatue2(stuId);
        if (flag==1){
            return CommonResult.success("修改状态成功");
        }else {
            return CommonResult.failed("修改状态失败");
        }
    }

}
